package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sys-manager/models"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	news := models.Newspaper{
		Title:   title,
		Content: content,
	}
	err := models.DB.Create(&news).Debug().Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func List(c *gin.Context) {
	var news []models.Newspaper
	err := models.DB.Find(&news).Debug().Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "查询成功",
			"data": news,
		})
		return
	}
}
